package org.overmind.ieat.server.controllers;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.overmind.ieat.server.model.ExceptionInfo;
import org.overmind.ieat.server.factory.ExceptionInfoFactory;
import org.springframework.beans.TypeMismatchException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.NoHandlerFoundException;

import javax.servlet.http.HttpServletRequest;

/**
 * @author eugene.karanda
 */
@ControllerAdvice //(annotations = RestController.class)
public class GlobalExceptionProcessor {
    private static Logger logger = LoggerFactory.getLogger(GlobalExceptionProcessor.class);

    //TODO DatabaseException BusinessLogicException etc

    @Autowired
    private ExceptionInfoFactory exceptionInfoFactory;

    /*@ResponseBody
    @ExceptionHandler(Exception.class)
    @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
    public ExceptionInfo databaseException(HttpServletRequest request, Exception exception) {
        logger.error(exception.getClass().getName());
        return new ExceptionInfo(request.getRequestURL().toString(),
                exception.getMessage());
    }*/

    @ResponseBody
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    @ResponseStatus(value = HttpStatus.NOT_FOUND)
    public ExceptionInfo methodNotSupportedException(HttpServletRequest request, HttpRequestMethodNotSupportedException exception) {
        return exceptionInfoFactory.createExceptionInfo(request, "exception.resourceNotFound");
    }

    @ResponseBody
    @ExceptionHandler(NoHandlerFoundException.class)
    @ResponseStatus(value = HttpStatus.NOT_FOUND)
    public ExceptionInfo noHandlerFoundException(HttpServletRequest request, NoHandlerFoundException exception) {
        return exceptionInfoFactory.createExceptionInfo(request, "exception.resourceNotFound");
    }

    @ResponseBody
    @ExceptionHandler(TypeMismatchException.class)
    @ResponseStatus(value = HttpStatus.BAD_REQUEST)
    public ExceptionInfo typeMismatchException(HttpServletRequest request, TypeMismatchException exception) {
        return exceptionInfoFactory.createExceptionInfo(request, "exception.badParameterType",
                exception.getRequiredType().toString());
    }

    @ResponseBody
    @ExceptionHandler(MethodArgumentNotValidException.class)
    @ResponseStatus(value = HttpStatus.BAD_REQUEST)
    public ExceptionInfo methodArgumentException(HttpServletRequest request, MethodArgumentNotValidException exception) {
        return exceptionInfoFactory.createExceptionInfo(request, "exception.badArgumentState"); //TODO with params
    }

    //TODO org.springframework.security.authentication.AuthenticationCredentialsNotFoundException
}
